package com.lanou.bookstore.order.web.servlet;

import com.lanou.bookstore.order.dao.OrderDao;
import com.lanou.bookstore.order.dao.OrderDaoImpl;
import com.lanou.bookstore.order.service.OrderSercice;
import com.lanou.bookstore.order.service.OrderSerciceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/osc")
public class OrderStateChange extends HttpServlet {

    OrderSercice os = new OrderSerciceImpl();
    OrderDao od = new OrderDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String oid = req.getParameter("oid");
        String para = req.getParameter("para");
        System.out.println(para);

        if ("pay".equals(para)) {
            req.getSession().setAttribute("oid", oid);
            req.getRequestDispatcher("/jsps/order/desc.jsp").forward(req, resp);
        }
        // 更改订单状态, 付款
        if ("payed".equals(para)) {
            od.changeOrderState(1, oid);
            //resp.sendRedirect("/oas");
            req.getRequestDispatcher("/oas").forward(req, resp);
        }

        // 更改订单状态, 确认收货
        if ("delivery".equals(para)) {
            od.changeOrderState(3, oid);
            int i = os.queryOid(oid);


            try {
                throw new Exception("确认成功,订单完成!!");
            } catch (Exception e) {
                e.printStackTrace();
                req.getSession().setAttribute("msgg", e.toString());
                req.getRequestDispatcher("/jsps/order/msg.jsp").forward(req, resp);
            }


//            if (i == 3){
//                try {
//                    throw new Exception("订单确认失败,你不是好人!!");
//                } catch (Exception e) {
//                    e.printStackTrace();
//
//                    req.getSession().setAttribute("msg",e.toString());
//                    req.getSession().setAttribute("relogin","islogin");
//                    req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
//                }
//            }else {
//                try {
//                    throw new Exception("确认成功,订单完成!!");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    req.getSession().setAttribute("relogin","islogin");
//                    req.getSession().setAttribute("msg",e.toString());
//                    req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
//                }
//            }


        }


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }
}
